package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.CarBrand;
import com.zhentao.pojo.CarModel;
import com.zhentao.pojo.VehicleInfo;
import com.zhentao.service.CarBrandService;
import com.zhentao.service.CarModelService;
import com.zhentao.service.VehicleInfoService;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/vehicle")
public class VehicleController {

    @Autowired
    private CarBrandService brandService;

    @Autowired
    private CarModelService modelService;

    @Autowired
    private VehicleInfoService vehicleInfoService;

    // 品牌管理
    @RequestMapping("/brand/list")
    public Result getBrandList(@RequestBody com.zhentao.pojo.Page page) {
        Page<CarBrand> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<CarBrand> queryWrapper = new QueryWrapper<>();
        Page<CarBrand> result = brandService.page(page1,queryWrapper);
        return Result.OK(result);
    }

    @RequestMapping("/brand/add")
    public Result addBrand(@RequestBody CarBrand brand) {
        brandService.save(brand);
        return Result.OK();
    }

    @RequestMapping("/brand/update")
    public Result updateBrand(@RequestBody CarBrand brand) {
        brandService.updateById(brand);
        return Result.OK();
    }

    @RequestMapping("/brand/delete/{id}")
    public Result deleteBrand(@PathVariable Long id) {
        brandService.removeById(id);
        return Result.OK();
    }

    // 型号管理
    @RequestMapping("/model/list")
    public Result getModelList(@RequestBody com.zhentao.pojo.Page page) {
        Page<CarModel> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<CarModel> queryWrapper = new QueryWrapper<>();
        Page<CarModel> result = modelService.page(page1,queryWrapper);
        return Result.OK(result);
    }

    @RequestMapping("/model/add")
    public Result addModel(@RequestBody CarModel model) {
        modelService.save(model);
        return Result.OK();
    }

    @RequestMapping("/model/update")
    public Result updateModel(@RequestBody CarModel model) {
        modelService.updateById(model);
        return Result.OK();
    }

    @RequestMapping("/model/delete/{id}")
    public Result deleteModel(@PathVariable Long id) {
        modelService.removeById(id);
        return Result.OK();
    }

    // 车辆信息管理
    @RequestMapping("/info/list")
    public Result getVehicleList(@RequestBody com.zhentao.pojo.Page page) {
        Page<VehicleInfo> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<VehicleInfo> queryWrapper = new QueryWrapper<>();
        Page<VehicleInfo> result = vehicleInfoService.page(page1,queryWrapper);
        return Result.OK(result);
    }

    @RequestMapping("/info/add")
    public Result addVehicle(@RequestBody VehicleInfo vehicle) {
        vehicleInfoService.save(vehicle);
        return Result.OK();
    }

    @RequestMapping("/info/update")
    public Result updateVehicle(@RequestBody VehicleInfo vehicle) {
        vehicleInfoService.updateById(vehicle);
        return Result.OK();
    }

    @RequestMapping("/info/delete/{id}")
    public Result deleteVehicle(@PathVariable Long id) {
        vehicleInfoService.removeById(id);
        return Result.OK();
    }
}
